<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>A custom no-value policy - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/no-value.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/no-value.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/no-value/builtin.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">A custom no-value policy</h1></div></div></div>
  <p>If you want your <code>basic_outcome&lt;&gt;</code> or <code>basic_result&lt;&gt;</code> instances to call
<code>std::abort()</code> whenever <code>.value()</code> is called on an object that does not contain a value, or <code>.error()</code> is called on an object that does not contain an error, you will need to define your own no-value policy as follows:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">struct</span> <span class="nl">abort_policy</span> <span class="p">:</span> <span class="n">outcome</span><span class="o">::</span><span class="n">policy</span><span class="o">::</span><span class="n">base</span>
<span class="p">{</span>
  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">Impl</span><span class="o">&gt;</span> <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">void</span> <span class="n">wide_value_check</span><span class="p">(</span><span class="n">Impl</span> <span class="o">&amp;&amp;</span><span class="n">self</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">base</span><span class="o">::</span><span class="n">_has_value</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">Impl</span><span class="o">&gt;</span><span class="p">(</span><span class="n">self</span><span class="p">)))</span>
      <span class="n">std</span><span class="o">::</span><span class="n">abort</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">Impl</span><span class="o">&gt;</span> <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">void</span> <span class="n">wide_error_check</span><span class="p">(</span><span class="n">Impl</span> <span class="o">&amp;&amp;</span><span class="n">self</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">base</span><span class="o">::</span><span class="n">_has_error</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">Impl</span><span class="o">&gt;</span><span class="p">(</span><span class="n">self</span><span class="p">)))</span>
      <span class="n">std</span><span class="o">::</span><span class="n">abort</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="err"> </span><span class="nc">Impl</span><span class="o">&gt;</span> <span class="k">static</span> <span class="k">constexpr</span> <span class="kt">void</span> <span class="n">wide_exception_check</span><span class="p">(</span><span class="n">Impl</span> <span class="o">&amp;&amp;</span><span class="n">self</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">base</span><span class="o">::</span><span class="n">_has_exception</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">forward</span><span class="o">&lt;</span><span class="n">Impl</span><span class="o">&gt;</span><span class="p">(</span><span class="n">self</span><span class="p">)))</span>
      <span class="n">std</span><span class="o">::</span><span class="n">abort</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">};</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/policies.cpp#L36" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>All policies ought to inherit from <a href="../../../reference/policies/base.html" class="api-reference"><code>base</code></a>
 in order to provide your policy implementation with
the internal policy API for accessing and manipulating <code>result</code> and <code>outcome</code> state.</p>

<p>Once the policy is defined, you have to specify it when providing your own
<code>basic_outcome</code> specialization:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">T</span><span class="o">&gt;</span>
<span class="k">using</span> <span class="n">strictOutcome</span> <span class="o">=</span>  <span class="c1">//
</span><span class="c1"></span><span class="n">outcome</span><span class="o">::</span><span class="n">basic_outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">exception_ptr</span><span class="p">,</span> <span class="n">abort_policy</span><span class="o">&gt;</span><span class="p">;</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/policies.cpp#L94" class="code-snippet-url" target="_blank">View this code on Github</a></div>



        </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/no-value.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/no-value.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/no-value/builtin.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
